package com.example.service;

import com.example.mapper.StatisticsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;

    public Map<String, Object> getHomeOverview() {
        Map<String, Object> statistics = new HashMap<>();
        
        // 获取酒店总数和增长率
        Integer hotelCount = statisticsMapper.getHotelCount();
        Double hotelIncrease = statisticsMapper.getHotelIncreaseRate();
        
        // 获取今日预订数和趋势
        Integer todayBookings = statisticsMapper.getTodayBookingsCount();
        Double bookingTrend = statisticsMapper.getBookingTrend();
        
        // 获取用户统计
        Integer userCount = statisticsMapper.getUserCount();
        Integer newUsers = statisticsMapper.getNewUsersThisWeek();
        
        // 获取营业额统计
        Double revenue = statisticsMapper.getTotalRevenue();
        Double revenueTrend = statisticsMapper.getRevenueTrend();

        statistics.put("hotelCount", hotelCount);
        statistics.put("hotelIncrease", hotelIncrease);
        statistics.put("todayBookings", todayBookings);
        statistics.put("bookingTrend", bookingTrend);
        statistics.put("userCount", userCount);
        statistics.put("newUsers", newUsers);
        statistics.put("revenue", revenue);
        statistics.put("revenueTrend", revenueTrend);

        return statistics;
    }
    
    public Map<String, Object> getOverview() {
        Map<String, Object> overview = new HashMap<>();
        
        // 获取总预订数
        Integer totalBookings = statisticsMapper.getTotalBookingsCount();
        
        // 获取今日预订数
        Integer todayBookings = statisticsMapper.getTodayBookingsCount();
        
        // 获取总房间数
        Integer totalRooms = statisticsMapper.getTotalRoomsCount();
        
        // 获取入住率
        Double occupancyRate = statisticsMapper.getOccupancyRate();
        
        overview.put("totalBookings", totalBookings);
        overview.put("todayBookings", todayBookings);
        overview.put("totalRooms", totalRooms);
        overview.put("occupancyRate", occupancyRate);
        
        return overview;
    }
    
    public Map<String, Object> getBookingTrend(String timeRange) {
        List<String> dates;
        List<Integer> counts;
        
        if ("week".equals(timeRange)) {
            dates = statisticsMapper.getLast7Days();
            counts = statisticsMapper.getBookingsCountByLast7Days();
        } else {
            dates = statisticsMapper.getLast30Days();
            counts = statisticsMapper.getBookingsCountByLast30Days();
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("dates", dates);
        result.put("counts", counts);
        
        return result;
    }
    
    public List<Map<String, Object>> getRoomTypeDistribution() {
        return statisticsMapper.getRoomTypeDistribution();
    }
    
    public Map<String, Object> getHotelRanking() {
        Map<String, Object> result = new HashMap<>();
        
        List<String> hotels = statisticsMapper.getTopHotelsName();
        List<Integer> bookings = statisticsMapper.getTopHotelsBookingCount();
        
        result.put("hotels", hotels);
        result.put("bookings", bookings);
        
        return result;
    }
} 